package org.skyeye.modeling.model;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.skyeye.modeling.tools.STextPropertyDescriptor;
/**
 * Root
 * @author Administrator
 *
 */
public class MRoot extends MBase {

	final public static String PROP_AUTHOR = "AUTHOR";
	
	private Map<String,Integer> nameMap = new HashMap<String,Integer>();
	
	private String author = "";

	protected IPropertyDescriptor[] descriptors = new IPropertyDescriptor[] {
			new STextPropertyDescriptor(PROP_AUTHOR, "author")
	};

	public MRoot(){
		this.setAuthor("somebody"); 
	}

	public IPropertyDescriptor[] getPropertyDescriptors() {
		return descriptors;
	}
	
	public Object getPropertyValue(Object id) {
		if (PROP_AUTHOR.equals(id))
			return this.getAuthor();

		return ""; 
	}

	public void setPropertyValue(Object id, Object value) {
		if (PROP_AUTHOR.equals(id))
			this.setAuthor((String)value);
	}

	public void setAuthor(String au) {
		this.author = au;
	}

	public String getAuthor() {
		return this.author;
	}

	public void addFChildOnly(MBase child){
		child.setRoot(this.root);
		for(Iterator iter = child.children.iterator();iter.hasNext();){
			MBase c = (MBase)iter.next();
			c.setRoot(this.root);
		}
		getChildren().add(child);
		child.setParent(this);
		this.firePropertyChange(PRO_CHILD,null, child);
	}
	public Map<String, Integer> getNameMap() {
		return nameMap;
	}

	public void setNameMap(Map<String, Integer> nameMap) {
		this.nameMap = nameMap;
	}

	public String getDeviceName(String name){
		int sn = 0;
		if(nameMap.containsKey(name)){
			int cur = nameMap.get(name);
			sn = cur+1;
		}
        nameMap.put(name, sn);
        String realName = name+"_"+sn;
        return realName;
	}
}
